import {Component, ChangeDetectorRef, ElementRef} from '@angular/core';

import {
    LoadingController,
    AlertController,
    IonicPage,
    NavController,
    NavParams,
    ToastController
} from 'ionic-angular';
import {ClientClaimProvider} from "../../../providers/client/client-claim/client-claim.provider";
import {SideMenuService} from "../../../services/common/side-menu.service";
import $ from "jquery";
import {LoadingService} from "../../../services/common/loading.service";


@IonicPage()
@Component({
    selector: 'page-client-claim',
    templateUrl: 'client-claim.html',
    providers: [
        ClientClaimProvider,
        SideMenuService
    ]
})

export class ClientClaimPage {

    //理赔列表
    private claimList: any = null;

    //理赔状态
    private claimStatus: any = [
        {code: "T", name: '预审通过', color: 'yellow'},
        {code: "E", name: '通融赔付', color: 'green'},
        {code: "D", name: '拒绝理赔', color: 'red'},
        {code: "A", name: '同意理赔', color: 'green'},
        {code: "I", name: '理赔关闭', color: 'red'},
        {code: "P", name: '进行中', color: 'yellow'}
    ];
    //当前客户id
    private currentCustomerId: string = null;

    private $window: any = $(window);
    private $document: any = $(document);
    private windowHeight = this.$window.height();

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public claimProvider: ClientClaimProvider,
                public loadingCtrl: LoadingController,
                public alertCtrl: AlertController,
                private sideMenuService: SideMenuService,
                public ref: ElementRef,
                public depChange: ChangeDetectorRef,
                public toast: ToastController,
                private load: LoadingService,
                ) {}

    ionViewDidLoad() {
        this.currentCustomerId = this.navParams.get('clientNum');
        this.getClaimListData();
    }

    /**
     * 获取理赔列表
     */
    getClaimListData(){
        this.load.showLoading();
        this.claimProvider.getClaimList(this.currentCustomerId).subscribe(item => {
            this.claimList = item;
            this.claimList.map((e, index) => {
                //是否展开
                e.isOpen = false;

                //是否固定
                e.isFixed = false;
                if (e.claims && e.claims.length) {
                    e.claims.map(i => {
                        this.claimStatus.map(x => {
                            if (i.claimsStatus == x.code) {
                                i.claimsStatusCn = x.name;
                            }
                        });
                    });
                }
                //默认第一个展开
                if (index === 0) {
                    e.isOpen = true;
                }
            });
            this.depChange.detectChanges();
            this.load.dismissLoading();
        },err=>{
            this.load.dismissLoading();
        });
    }

    /**
     *  toast 提示信息
     * @param {string} message 提示内容
     */
    toastCtrl(message: string) {
        this.toast.create({
            message: message,
            position: 'middle',
            duration: 1200
        }).present();
    }

    /**
     * 判断是否支持css3属性
     * @param property
     * @param value
     * @param noPrefixes
     * @returns {boolean}
     */
    featureTest(property, value, noPrefixes) {
        let prop = property + ':',
            el = document.createElement('test'),
            mStyle = el.style;

        if (!noPrefixes) {
            mStyle.cssText = prop + ['-webkit-', '-moz-', '-ms-', '-o-', ''].join(value + ';' + prop) + value + ';';
        } else {
            mStyle.cssText = prop + value;
        }
        return mStyle[property].indexOf(value) !== -1;
    }

    /**
     * 滚动监听
     * @param event
     */
    scrolling(event) {
        // if (this.featureTest('position', 'sticky', true)) return;
        //
        // const itemList = this.ref.nativeElement.querySelectorAll('.claim-item');
        // let scrollTop = event.scrollTop, documentHeight = this.$document.height(),
        //     dwh = documentHeight - this.windowHeight,
        //     extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
        // for (let i = 0; i < itemList.length; i++) {
        //     let s = itemList[i];
        //     let elementTop = $(s).offset().top;
        //     let etse = elementTop - extra;
        //     if (scrollTop <= etse) {
        //         this.claimList[i].isFixed = false;
        //         // console.log(`剩下的：${i}`);
        //     } else {
        //         this.claimList[i].isFixed = true;
        //         // console.log(`第几个：${i}`);
        //     }
        // }
        // this.depChange.detectChanges();
    }

    /**
     * 跳转到保单详情
     * @param {string} policyID
     */
    goToDetail(policyID: string) {
        this.navCtrl.push('PolicyDetailsPage', {
            policyNO: policyID,
            agentCode:window.localStorage.getItem('userName') || ''
        });
    }

    /**
     * 收起列表
     * @param e
     */
    itemUp(e) {
        e.isOpen = !e.isOpen;
    }

    /**
     * 展开列表
     * @param e
     */
    itemDown(e) {
        e.isOpen = !e.isOpen;
    }



    /**
     * 菜单
     * @param myEvent
     */
    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent);
    }

    /**
     *  返回
     */
    backButtonClick() {
        this.navCtrl.pop(null);
    }

}
